﻿using System;
using System.IO;
using System.Text;
using System.Web.UI;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Xpedite.Resources;
using Xpedite.Resources.Builders;
using Xpedite.Resources.Combiners;
using Xpedite.Resources.Definitions;
using IResourceUrlGenerator = Xpedite.Resources.IResourceUrlGenerator;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Test.Resources.Definitions
{
  [TestClass]
  public class SpriteDefinitionTest
  {
    private readonly Mock<IResourceBuilderFactory> _builderFactory = new Mock<IResourceBuilderFactory>(MockBehavior.Strict);
    private readonly Mock<IResourceUrlGenerator> _urlGenerator = new Mock<IResourceUrlGenerator>(MockBehavior.Strict);
    private readonly Mock<ICombinerFactory> _combinerFactory = new Mock<ICombinerFactory>(MockBehavior.Strict);
    private readonly Mock<IResourceContainer> _resource1 = new Mock<IResourceContainer>(MockBehavior.Strict);
    private readonly Mock<IResourceContainer> _resource2 = new Mock<IResourceContainer>(MockBehavior.Strict);
    private readonly Mock<IResourceContainer> _resource3 = new Mock<IResourceContainer>(MockBehavior.Strict);
    private readonly Mock<IResourceCache> _cache = new Mock<IResourceCache>(MockBehavior.Strict);
    private readonly Mock<ICombiner> _combiner1 = new Mock<ICombiner>(MockBehavior.Strict);
    private readonly Mock<ICombiner> _combiner2 = new Mock<ICombiner>(MockBehavior.Strict);
    private readonly DefinitionBase _definition;

    public SpriteDefinitionTest()
    {
      _definition = new SpriteDefinition(new DefinitionArgs(_cache.Object, _urlGenerator.Object, _builderFactory.Object, _combinerFactory.Object), "ReferenceName", ResourceType.Png);
    }

    [TestMethod]
    public void TestResourceType()
    {
      Assert.AreEqual(ResourceType.Png, new SpriteDefinition(new DefinitionArgs(_cache.Object, _urlGenerator.Object, _builderFactory.Object, _combinerFactory.Object), "ReferenceName", ResourceType.Png).ResourceType);
      Assert.AreEqual(ResourceType.Jpeg, new SpriteDefinition(new DefinitionArgs(_cache.Object, _urlGenerator.Object, _builderFactory.Object, _combinerFactory.Object), "ReferenceName", ResourceType.Jpeg).ResourceType);
      Assert.AreEqual(ResourceType.Gif, new SpriteDefinition(new DefinitionArgs(_cache.Object, _urlGenerator.Object, _builderFactory.Object, _combinerFactory.Object), "ReferenceName", ResourceType.Gif).ResourceType);
    }

    [TestMethod]
    public void TestEmitResource()
    {
      var capturedSpriteFactory = new CapturedValue<Func<IResourceContainer>>();
      var capturedGifFactory = new CapturedValue<Func<IResourceContainer>>();
      var capturedCssFactory = new CapturedValue<Func<IResourceContainer>>();
      var transparentGifUrl = CreateResourceUrl("/xpedite.sprite.xcr");
      var spriteUrl = CreateResourceUrl("/xpedite.image.xcr");
      var cssUrl = CreateResourceUrl("/xpedite.css.xcr");
      var html = new StringBuilder();

      //Setup TransparentGif Caching
      _urlGenerator.Setup(mock => mock.GenerateUrlFor("/xpedite.sprite.xcr")).Returns(transparentGifUrl);
      _cache.Setup(mock => mock.GetOrCreateWithNoDependency(transparentGifUrl, It.Is(capturedGifFactory.Match))).Returns(_resource1.Object);

      //Setup Sprite Caching
      _urlGenerator.Setup(mock => mock.GenerateUrlFor(_definition)).Returns(spriteUrl);
      _cache.Setup(mock => mock.GetOrCreateWithResourceDependencies(spriteUrl, It.Is(capturedSpriteFactory.Match), _definition.Resources)).Returns(_resource2.Object);

      //Setup Sprite Caching
      _urlGenerator.Setup(mock => mock.GenerateUrlFor(_definition, ResourceType.Css)).Returns(cssUrl);
      _cache.Setup(mock => mock.GetOrCreateWithResourceDependencies(cssUrl, It.Is(capturedCssFactory.Match), _definition.Resources)).Returns(_resource3.Object);

      using (var stringWriter = new StringWriter(html))
      using (var htmlWriter = new HtmlTextWriter(stringWriter))
        _definition.EmitResource(htmlWriter);

      //Verify Emit
      Assert.AreEqual("<link type=\"text/css\" rel=\"stylesheet\" href=\"/xpedite.css.xcr\" />", html.ToString());
      Assert.IsNotNull(capturedGifFactory.Value);
      Assert.IsNotNull(capturedSpriteFactory.Value);
      Assert.IsNotNull(capturedCssFactory.Value);

      //Verify TransparentGif Create
      var resourceContainer = capturedGifFactory.Value.Invoke();
      Assert.AreEqual(ResourceType.Gif, resourceContainer.ResourceType);
      Assert.IsNotNull(resourceContainer.Resource);
      Assert.AreEqual(transparentGifUrl, resourceContainer.ResourceUrl);

      //Verify Sprite Create
      _combinerFactory.Setup(mock => mock.CreateSpriteCombiner(ResourceType.Png)).Returns(_combiner1.Object);
      _combiner1.Setup(mock => mock.Combine(spriteUrl, _definition)).Returns(_resource2.Object);
      capturedSpriteFactory.Value.Invoke();

      //Verify Sprite Css Create
      _combinerFactory.Setup(mock => mock.CreateSpriteCssCombiner()).Returns(_combiner2.Object);
      _combiner2.Setup(mock => mock.Combine(spriteUrl, _definition)).Returns(_resource3.Object);
      capturedCssFactory.Value.Invoke();
    }

    private IResourceUrl CreateResourceUrl(String url)
    {
      var resourceUrl = new Mock<IResourceUrl>(MockBehavior.Strict);

      resourceUrl.SetupGet(mock => mock.VirtualPath).Returns(url);

      return resourceUrl.Object; 
    }
  }
}
